{
 "metadata": {
  "signature": "sha256:aeeba25e9e6d0e7f4715199db2b1451cf82b44d3d72bf81e955c72cd39c99eab"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Matplotlib: qt with ipython and designer\n",
      "======================================================================\n",
      "\n",
      "Example code for embedding Matplotlib in Qt applications is given at\n",
      "[embedding\\_in\\_qt.py](http://cvs.sourceforge.net/viewcvs.py/matplotlib/matplotlib/examples/).\n",
      "This recipe extends that basic formula for integration with other\n",
      "powerful tools. In particular, we bring together the use of the GUI\n",
      "creation tool made by [Trolltech](http://www.trolltech.com) (the\n",
      "creators of Qt) and the ability to interact with a running Qt\n",
      "application via [IPython](http://ipython.scipy.org).\n",
      "\n",
      "A basic tutorial for using Designer (along with the pertinent system\n",
      "requirements) has been posted by [Alex\n",
      "Fedosov](http://www.cs.usfca.edu/~afedosov/qttut/). Please review it\n",
      "before continuing.\n",
      "\n",
      "However, for our purposes, we will create a much simpler design in\n",
      "Designer.\n",
      "\n",
      "Open Designer, and create a new \"Main Window\":\n",
      "\n",
      "![](files/attachments/Matplotlib_Qt_with_IPython_and_Designer/designer_newopen.png)\n",
      "\n",
      "When the wizard appears, remove all of the menus and toolbars that it\n",
      "suggests to generate:\n",
      "\n",
      "![](files/attachments/Matplotlib_Qt_with_IPython_and_Designer/designer_wizard.png)\n",
      "\n",
      "Now add a custom widget to your project as:\n",
      "\n",
      "![](files/attachments/Matplotlib_Qt_with_IPython_and_Designer/designer_edit_custom_widgets.png)\n",
      "\n",
      "Give this new widget the name of and set the size to 400, 300:\n",
      "\n",
      "![](files/attachments/Matplotlib_Qt_with_IPython_and_Designer/designer_new_widget.png)\n",
      "\n",
      "The should now appear in your toolbox. Click on it and then click in the\n",
      "Form that has been created. Also perform \"Lay Out Vertically (Ctrl+L)\"\n",
      "and \"Adjust Size (Ctrl+J)\" operations on the Form (without the widget\n",
      "being selected) if you know what those are. At this point your workspace\n",
      "might look something like this:\n",
      "\n",
      "![](files/attachments/Matplotlib_Qt_with_IPython_and_Designer/designer_full_workspace.png)\n",
      "\n",
      "Now we need to get the \"import\" setting put in, as specified\n",
      "[here](http://www.river-bank.demon.co.uk/docs/pyqt/PyQt.html#AEN95):\n",
      "\n",
      "![](files/attachments/Matplotlib_Qt_with_IPython_and_Designer/designer_form_settings_comment.png)\n",
      "\n",
      "Of course, you'll also need the file that will then be included:\n",
      "[mplwidget.py](files/attachments/Matplotlib_Qt_with_IPython_and_Designer/mplwidget.py).\n",
      "\n",
      "The product of all of these operations in Designer is a .ui file. So,\n",
      "save the Form we've been working on and call it\n",
      "\"mplwidget\\_tutorial.ui\".\n",
      "\n",
      "One of the things that can mess this up is that Designer automatically\n",
      "increments the names given to widget instances (Form1, Form2,\n",
      "matplotlibWidget1, matplotlibWidget2, etc.) so if those don't match up\n",
      "with what I used, you may need to make some logical adjustments to your\n",
      "procedure.\n",
      "\n",
      "Now, we use the tool pyuic (included with\n",
      "[PyQt](http://www.riverbankcomputing.co.uk/pyqt/index.php) to convert\n",
      "this .ui file into a Python class. This is easily accomplished with:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "pyuic mplwidget_tutorial.ui > mplwidget_tutorial.py"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Go ahead and view the contents of mplwidget\\_tutorial.py and compare it\n",
      "with what [I got](files/attachments/Matplotlib_Qt_with_IPython_and_Designer/mplwidget_tutorial.py). You can also look\n",
      ")# at my [mplwidget_tutorial.ui](files/attachments/Matplotlib_Qt_with_IPython_and_Designer/mplwidget_tutorial.ui)\n",
      "\n",
      "Now, it's nice to have the invocation written up in its own\n",
      "[main_mpl_tutorial.py](files/attachments/Matplotlib_Qt_with_IPython_and_Designer/main_mpl_tutorial.py) file, which is amazingly short:\n",
      ")# "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from mplwidget_tutorial import *\n",
      "\n",
      "f = Form1()\n",
      "f.show()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Then, we want to start ipython and instantiate the window.  In order to succeed in this endeavor, there are a bunch of packages (each with minimum version requirements) you'll need, like python2.3-ipython, python2.3-qt3, and so on.  The most important one, though, is ipython >= 0.6.13 (I think).  From that version on, there is a super-great feature that adds an invocation switch {{{-qthread}}} that starts a QApplication in a separate thread, in such a way that the ipython prompt can still interact with it.\n",
      "\n",
      "![](files/attachments/Matplotlib_Qt_with_IPython_and_Designer/ipython_invoked.png)\n",
      "\n",
      "So, if the above command doesn't work for you, check the version.  It may also be invoked as \"python2.3-ipython\" depending on your configuration.\n",
      "\n",
      "Okay, now comes the coolest part: the interaction.\n",
      "\n",
      "![](files/attachments/Matplotlib_Qt_with_IPython_and_Designer/ipython_interacted.png)\n",
      "\n",
      "And, it is very easy to go back to Designer, add a button, re-run pyuic, and you've got another version.\n",
      "\n"
     ]
    }
   ],
   "metadata": {}
  }
 ]
}